import {
  Entity,
  PrimaryKey,
  Property,
  ManyToOne,
  Reference,
  wrap,
  Type,
} from "@mikro-orm/core";
import { BaseEntity } from "./Base";
import { User } from "./User";
import { App } from "./App";
import { Dataset } from "./Dataset";

@Entity()
export class Endpoint extends BaseEntity {
  @Property({ type: "string" })
  name: string;

  @Property({ type: "string" })
  type: string; // delete, update, query, create

  @Property({ type: "string", nullable: true })
  introduce: string;

  @Property({ type: "integer", default: 0, nullable: true })
  isOutputCsv: number;

  // @Property({ type: JsonType })
  @Property({ type: "string" })
  value: string; // app secret for get this value

  // @ManyToOne({ entity: () => App, nullable: true })
  @ManyToOne({ entity: () => Dataset })
  dataset!: Reference<Dataset>; // response to filed 'dataset_id'

  @ManyToOne({ entity: () => User })
  creator!: Reference<User>; // response to filed 'creator_id'

  @ManyToOne({ entity: () => User })
  editor!: Reference<User>; // response to filed 'editor_id'

  constructor({
    name,
    value,
    introduce = "",
    dataset,
    creator,
    editor,
    type,
    isOutputCsv,
  }: {
    name: string;
    type: string;
    value: any;
    introduce: string;
    creator: Reference<User>;
    editor: Reference<User>;
    dataset: Reference<Dataset>;
    isOutputCsv: number;
  }) {
    super();
    this.name = name;
    this.value = value;
    this.introduce = introduce;
    this.dataset = dataset;
    this.type = type;
    this.creator = creator;
    this.editor = editor;
    this.isOutputCsv = Number(isOutputCsv);
  }
}
